package lx;

import java.io.*;
 /*
        四种方式拷贝文件，并统计各自用时
*/


public class demo1 {
    public static void main(String[] args) throws IOException {
        File src = new File("D:\\tep\\省赛视频");
        File dest = new File("D:\\副本");
        long start = System.currentTimeMillis();
        //1.字节流的基本流：一次读写一个字节1,162,382,123 字节
       // method01(src, dest);

        //2.字节流的基本流：一次读写一个字节数组
      // method02(src, dest);//2.526秒

        //3.字节流的缓冲流：一次读写一个字节
       // method03(src, dest);//16.641秒

        //4.字节流的缓冲流：一次读写一个字节数组
       method04(src, dest);//2.657秒
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000.0 + "秒");
    }


    /*
     * 作用：拷贝文件夹
     * 参数一：数据源
     * 参数二：目的地
     *
     * */
    private static void method01(File src, File dest) throws IOException {
        dest.mkdirs();
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if (file.isFile()) {
                //3.判断文件，拷贝
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(dest, file.getName()));
                int len = 0;
                while ((len = fis.read()) != -1) {
                    fos.write(len);
                }
                fos.close();
                fis.close();

            } else {
                //4.判断文件夹，递归
                method01(file, new File(dest, file.getName()));
            }


        }


    }
    private static void method02(File src, File dest) throws IOException {
        dest.mkdirs();
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if (file.isFile()) {
                //3.判断文件，拷贝
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(dest, file.getName()));
                int len = 0;
                byte[] bytes = new byte[8192];
                while ((len = fis.read(bytes)) != -1) {
                    fos.write(bytes);
                }
                fos.close();
                fis.close();

            } else {
                //4.判断文件夹，递归
                method02(file, new File(dest, file.getName()));
            }


        }


    }

    private static void method03(File src, File dest) throws IOException {
        dest.mkdirs();
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if (file.isFile()) {
                //3.判断文件，拷贝
                BufferedInputStream fis =new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream fos =new BufferedOutputStream(new FileOutputStream(new File(dest, file.getName())));
                int len = 0;
                while ((len = fis.read()) != -1) {
                    fos.write(len);
                }
                fos.close();
                fis.close();

            } else {
                //4.判断文件夹，递归
                method03(file, new File(dest, file.getName()));
            }


        }


    }

    private static void method04(File src, File dest) throws IOException {
        dest.mkdirs();
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if (file.isFile()) {
                //3.判断文件，拷贝
                BufferedInputStream fis =new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream fos =new BufferedOutputStream(new FileOutputStream(new File(dest, file.getName())));
                int len = 0;
                byte[] bytes = new byte[8192];
                while ((len = fis.read(bytes)) != -1) {
                    fos.write(bytes);
                }
                fos.close();
                fis.close();

            } else {
                //4.判断文件夹，递归
                method04(file, new File(dest, file.getName()));
            }


        }


    }
}


